//
//  PRTools.m
//  Lottery
//
//  Created by maihong on 2019/5/27.
//  Copyright © 2019 WYR. All rights reserved.
//

#import "PRTools.h"

#define MHUDTAG 1000000

@implementation PRTools

/**
 *  HUD
 */
+ (void)showProgressHUD {
    
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.frame = CGRectMake(0, 0, kSPScreenWidth
                                   , kSPScreenHeight);
    //    progressHud.transform = CGAffineTransformMakeRotation(M_PI_2);
    progressHud.tag = MHUDTAG;
    progressHud.dimBackground = NO;
    [progressHud setRemoveFromSuperViewOnHide:YES];
    [progressHud show:YES];
    [kSPAPP.window addSubview:progressHud];;
}

+ (void)showProgressHUD:(NSString *)string
{
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.frame = CGRectMake(0, 0, kSPScreenWidth
                                   , kSPScreenHeight);
    //    progressHud.transform = CGAffineTransformMakeRotation(M_PI_2);
    progressHud.tag = MHUDTAG;
    progressHud.dimBackground = NO;
    progressHud.labelText = string;
    [progressHud setRemoveFromSuperViewOnHide:YES];
    [progressHud show:YES];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)showProgressHUDWithText:(NSString *)string afterDelay:(NSTimeInterval)time
{
    [self showProgressHUDWithText:string font:[UIFont systemFontOfSize:15.f] afterDelay:time];
}

+ (void)showProgressHUDWithText:(NSString *)string font:(UIFont *)font afterDelay:(NSTimeInterval)time
{
    [PRTools hideProgressHUD];
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.tag = MHUDTAG;
    progressHud.dimBackground = NO;
    progressHud.mode = MBProgressHUDModeText;
    progressHud.labelText = string;
    progressHud.labelFont = font;
    [progressHud show:YES];
    [progressHud hide:YES afterDelay:time];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)showProgressHUDWithSucess:(NSString *)string afterDelay:(NSTimeInterval)time
{
    [PRTools showProgressHUDWithSucess:string font:[UIFont systemFontOfSize:15.f] afterDelay:time];
}

+ (void)showProgressHUDWithSucess:(NSString *)string font:(UIFont *)font afterDelay:(NSTimeInterval)time {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"PRImage" withExtension:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    UIImage* successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"pr_hud_success" ofType:@"png"]];
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.tag = MHUDTAG;
    progressHud.customView = [[UIImageView alloc] initWithImage:successImage];
    progressHud.labelText = NSLocalizedString(string, nil);
    progressHud.labelFont = font;
    [progressHud show:YES];
    [progressHud hide:YES afterDelay:time];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)showProgressHUDWithSucess:(NSString *)string subStr:(NSString *)subStr afterDelay:(NSTimeInterval)time {
    [self showProgressHUDWithSucess:string subStr:subStr font:[UIFont systemFontOfSize:15.f] afterDelay:time];
}
+ (void)showProgressHUDWithSucess:(NSString *)string subStr:(NSString *)subStr font:(UIFont *)font afterDelay:(NSTimeInterval)time {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"PRImage" withExtension:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    UIImage* successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"pr_hud_success" ofType:@"png"]];
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.tag = MHUDTAG;
    progressHud.customView = [[UIImageView alloc] initWithImage:successImage];
    progressHud.labelText = NSLocalizedString(string, nil);
    progressHud.detailsLabelText = NSLocalizedString(subStr, nil);
    progressHud.labelFont = font;
    progressHud.detailsLabelFont = [UIFont systemFontOfSize:font.pointSize * 0.8];
    [progressHud show:YES];
    [progressHud hide:YES afterDelay:time];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)showProgressHUDWithError:(NSString *)string afterDelay:(NSTimeInterval)time {
    [PRTools showProgressHUDWithError:string font:[UIFont systemFontOfSize:15.f] afterDelay:time];
}

+ (void)showProgressHUDWithError:(NSString *)string font:(UIFont *)font afterDelay:(NSTimeInterval)time {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"PRImage" withExtension:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    UIImage* errorImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"pr_hud_error" ofType:@"png"]];
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.tag = MHUDTAG;
    progressHud.customView = [[UIImageView alloc] initWithImage:errorImage];
    progressHud.labelText = NSLocalizedString(string, nil);
    progressHud.labelFont = font;
    [progressHud show:YES];
    [progressHud hide:YES afterDelay:time];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)showProgressHUDWithError:(NSString *)string subStr:(NSString *)subStr afterDelay:(NSTimeInterval)time {
    [self showProgressHUDWithError:string subStr:subStr font:[UIFont systemFontOfSize:15.f] afterDelay:time];
}
+ (void)showProgressHUDWithError:(NSString *)string subStr:(NSString *)subStr font:(UIFont *)font afterDelay:(NSTimeInterval)time {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"PRImage" withExtension:@"bundle"];
    NSBundle *imageBundle = [NSBundle bundleWithURL:url];
    UIImage* errorImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"pr_hud_error" ofType:@"png"]];
    MBProgressHUD *progressHud = [[MBProgressHUD alloc] initWithView:kSPAPP.window];
    progressHud.mode = MBProgressHUDModeCustomView;
    progressHud.tag = MHUDTAG;
    progressHud.customView = [[UIImageView alloc] initWithImage:errorImage];
    progressHud.labelText = NSLocalizedString(string, nil);
    progressHud.detailsLabelText = NSLocalizedString(subStr, nil);
    progressHud.labelFont = font;
    progressHud.detailsLabelFont = [UIFont systemFontOfSize:font.pointSize * 0.8];
    [progressHud show:YES];
    [progressHud hide:YES afterDelay:time];
    [kSPAPP.window addSubview:progressHud];
}

+ (void)hideProgressHUD {
    MBProgressHUD *progressHud = (MBProgressHUD *)[[UIApplication sharedApplication].keyWindow viewWithTag:MHUDTAG];
    if (progressHud) {
        [progressHud hide:YES];
        [progressHud removeFromSuperview];
    }
    [MBProgressHUD hideHUDForView:kSPAPP.window animated:YES];
}

+ (void)hideProgressHUD:(NSTimeInterval)time
{
    MBProgressHUD *progressHud = (MBProgressHUD *)[[UIApplication sharedApplication].keyWindow viewWithTag:MHUDTAG];
    if (progressHud) {
        [progressHud hide:YES afterDelay:time];
        //        [progressHud removeFromSuperview];
    }
    [MBProgressHUD hideHUDForView:kSPAPP.window animated:YES];
}



@end
